<?php

namespace app\Http\Controllers\admins;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Tag extends Controller
{
    // 表单
    private function form($field = [])
    {
        $form = [
            ['label' => '标签名称', 'name' => 'name', 'verify' => 'required', 'placeholder' => '请输入'],
            ['label' => '别名', 'name' => 'slug'],
            ['label' => 'seo标题', 'name' => 'seo_title'],
            ['label' => 'seo描述', 'name' => 'seo_des', 'type' => 'textarea'],
            ['label' => '蜘蛛不索引', 'name' => 'robots', 'type' => 'switch'],
            ['label' => '排序', 'name' => 'order', 'value' => 0, 'verify' => 'number', 'type' => 'number'],
            ['label' => '状态', 'name' => 'status', 'type' => 'radio', 'value' => 0, 'options' => [0 => '启用', 1 => '禁用']],
        ];
        if ($field) {
            if (count($field) === count($field, 1)) {
                array_push($form, $field);
            } else {
                $form = array_merge($form, $field);
            }
        }
        return $form;
    }

    public function index(Request $request)
    {
        if ($request->ajax()) {
            // $page = $request->page ?? 1;
            $limit = $request->limit ?? 10;
            $db = DB::table('tag');
            // 搜索
            if (isset($request->search) && $request->search) {
                foreach ((array)$request->search as $k => $v) {
                    if ($v) $db = $db->where($k, 'like', "%{$v}%");
                }
            }
            $db = $db->orderBy('id', 'asc')
                ->orderBy('order', 'desc');
            $data['count'] = $db->count();
            // 分页
            // $db = $db->paginate($limit)->items();
            if ($data['count']) {
                $data['code'] = 0;
            } else {
                $data['code'] = 1;
                $data['msg'] = '暂无记录';
            }
            $db = $db->get()->all();
            $data['data'] = array_map(function ($item) {
                $item->status = $item->status ? '禁用' : '启用';
                return $item;
            }, $db);
            return json_encode($data);
        }
        $data['title'] = '标签列表';
        $data['prefix'] = '/admins/tag';
        return view('/admins/tag/index', $data);
    }

    public function add()
    {
        $data['title'] = '添加';
        $form = $this->form([
            ['type' => 'submit', 'value' => '保存'],
            ['type' => 'button', 'value' => '取消', 'filter' => 'cancel', 'btn' => 'primary']
        ]);
        $args = ['url' => '/admins/tag/save', 'reload' => 'parent.location'];
        $data['form'] = $this->build_form($form);
        $data['script'] = $this->build_script($form, $args);
        return view('/admins/tag/add', $data);
    }

    public function save(Request $request)
    {
        $data = $this->check($this->form(), $request);
        if (!is_array($data)) return $data;
        $check = DB::table('tag')->where('name', $data['name'])->first();
        if ($check) return $this->errMsg(1, '标签已存在');
        if ($data['slug']) {
            $check = DB::table('tag')->where('slug', $data['slug'])->first();
            if ($check) return $this->errMsg(1, '标签重复');
        }
        $res = DB::table('tag')->insert($data);
        if (!$res) $this->errMsg(1, '保存失败');
        return $this->errMsg(0, '保存成功');
    }

    public function edit(Request $request)
    {
        $id = (int)$request->id;
        if (!$id) return $this->errMsg(1, '数据错误');
        $value = DB::table('tag')->where('id', $id)->first();
        $form = $this->form([
            ['name' => 'id', 'type' => 'hidden', 'value' => $id],
            ['type' => 'submit', 'value' => '保存'],
            ['type' => 'button', 'value' => '取消', 'filter' => 'cancel', 'btn' => 'primary']
        ]);
        $args = ['url' => '/admins/tag/save_edit', 'reload' => 'dataTable'];
        $data['title'] = '编辑';
        $data['form'] = $this->build_form($form, $value);
        $data['script'] = $this->build_script($form, $args);
        return view('/admins/tag/edit', $data);
    }

    public function save_edit(Request $request)
    {
        $id = (int)$request->id;
        if (!$id) return $this->errMsg(1, '数据错误');
        $data = $this->check($this->form(), $request);
        if (!is_array($data)) return $data;
        $check = DB::table('tag')->where('name', $data['name'])->first();
        if ($check && $check->name !== $data['name']) return $this->errMsg(1, '标签已存在');
        if ($data['slug']) {
            $check = DB::table('tag')->where('slug', $data['slug'])->first();
            if ($check && $check->slug !== $data['slug']) return $this->errMsg(1, '标签重复');
        }
        DB::table('tag')->where('id', $id)->update($data);
        return $this->errMsg(0, '修改成功');
    }

    public function del(Request $request)
    {
        $id = (array)$request->id;
        if (!$id) return $this->errMsg(1, '请选择数据');
        $res = DB::table('tag')->whereIn('id', $id)->delete();
        if (!$res) return $this->errMsg(1, '删除失败');
        return $this->errMsg(0, '删除成功');
    }
}
